﻿using System.Collections.Generic;
using System.Linq;

namespace Qy.Scenario;

public interface ICategoryEntity
{
    string Name { get; set; }
    int Id { get; set; }
    int Pid { get; set; }
    int Type { get; set; }
}

public class CategoryViewModel
{
    public int Id { get; set; }
    public int Pid { get; set; }
    public string Title { get; set; }
    public int Type { get; set; }
    public int Depth { get; set; }
    public int ChildrenCount { get { return Children.Count; } }
    public bool IsLast { get; set; }
    public bool Spread { get; set; } = true;
    public List<CategoryViewModel> Children { set; get; } = new();
}

public static partial class CategoryExtensions
{
    public static IEnumerable<CategoryViewModel> AsViewCategoryModels(this IEnumerable<ICategoryEntity> categoryEntities)
    {
        var viewModels = new List<CategoryViewModel>();
        foreach (var entity in categoryEntities)
        {
            viewModels.Add(new CategoryViewModel { Id = entity.Id, Title = entity.Name, Pid = entity.Pid, Type = entity.Type });
        }
        return viewModels;
    }

    /// <summary>
    /// 递归子级
    /// </summary>
    /// <param name="cates"></param>
    /// <param name="treeModels"></param>
    /// <param name="parentId"></param>
    /// <param name="depth"></param>
    public static void IndentedContentCates(IEnumerable<CategoryViewModel> cates, IList<CategoryViewModel> treeModels, int parentId = 0, int depth = 0)
    {
        depth++;
        var childrens = cates.Where(a => a.Pid == parentId).ToList();
        for (int i = 0; i < childrens.Count; i++)
        {
            childrens[i].Depth = depth;
            childrens[i].IsLast = i >= childrens.Count - 1;
            treeModels.Add(childrens[i]);
            IndentedContentCates(cates, childrens[i].Children, childrens[i].Id, depth);
        }
    }

    public static void ExpandContentCates(IEnumerable<CategoryViewModel> rootCates, IList<CategoryViewModel> expandModels)
    {
        foreach (var item in rootCates)
        {
            expandModels.Add(item);
            ExpandContentCates(item.Children, expandModels);
        }
    }

    public static IEnumerable<CategoryViewModel> AsCateListItems(this IEnumerable<ICategoryEntity> categoryEntities)
    {
        IEnumerable<CategoryViewModel> categories = categoryEntities.AsViewCategoryModels();
        List<CategoryViewModel> treeModels = new();
        IndentedContentCates(categories, treeModels);
        List<CategoryViewModel> expandModels = new();
        ExpandContentCates(treeModels, expandModels);
        foreach (var item in expandModels)
        {
            string headString = "";
            if (item.Depth > 1)
            {
                if (item.ChildrenCount > 0)
                    headString += "├ ";
                else
                {
                    if (item.IsLast)
                        headString += "└ ";
                    else
                        headString += "├ ";
                }
                for (int j = 2; j < item.Depth; j++)
                    headString += " ─ ";
            }
            item.Title = string.Format("{0}{1}", headString, item.Title);
        }
        return expandModels;
    }

}
